<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<style>
			.datepicker-input{height: 34px; border: 1px solid #D3D3D3; background-color: #fff; border-radius: 2px; width: 250px; padding: 0px 4px;}
			table.datagrid-ftable{background: beige;}
		</style>
		<div class="super-theme-example">
			<div class="options-toolbar">
				<form id="affilateReportDatagridQueryParams">
					<label>美元-新台币:</label><input class="easyui-textbox" name="twd$" value="30.511" data-options="width:120,prompt:'美元-新台币'">
					<label>美元-港元:</label><input class="easyui-textbox" name="hk$" value="7.8276" data-options="width:120,prompt:'美元-港币'">
					<label>美元-人民币:</label><input class="easyui-textbox" name="cny$" value="7.0353" data-options="width:120,prompt:'美元-人民币'">
					<select id="affilateReportShopIdCombo" class="easyui-combobox" name="shopId" style="width:150px;"></select>
					<input id="affilateReport-datepicker" class="datepicker-input" />
					<a href="javascript:void(0)" class="easyui-linkbutton primary" data-options="iconCls:'fa fa-search'" onclick="affilate_report.methods.initDatagrid();">查询</a>
				</form>
			</div>
			<table id="affilateReport-datagrid"></table>
		</div>
		<script type="text/javascript">
			var affilate_report = {
				methods : {
					initShopCombo: function(){
						$('#affilateReportShopIdCombo').combobox({
						    url: commonConfig.shopUrlPrefix() + '/shop/combobox',
						    valueField: 'id',
						    textField: 'text'
						});
					},
					initDatagridQueryParams: function(){
						var params = $('#affilateReportDatagridQueryParams').serializeArray();
						var queryParams = {};
						$.each(params , function(index , param){
						     queryParams[param.name] = param.value;
						})
						var time = $('#affilateReport-datepicker').val();
						queryParams.startTime = timestamp(time.substring(0, 19));
						queryParams.endTime = timestamp(time.substring(22));
						return queryParams;
					},
					initTimeSelect: function(){
						$('#affilateReport-datepicker').daterangepicker($.extend(true, {} , globalDaterangepickerOptions));
					},
					initDatagrid : function(){
						Promise.all([
                            requestServer(commonConfig.shopUrlPrefix() + '/admin/report/affilate/', 'GET', affilate_report.methods.initDatagridQueryParams()),
                            requestServer(commonConfig.shopUrlPrefix() + '/admin/report/repeatRate', 'GET', affilate_report.methods.initDatagridQueryParams())
                        ]).then((response)=>{
							var datagridData = {"rows": [] , "footer": []} ; 
							// 0
							if (response[0].code != 1) {
								$.messager.alert('提示',response[0].message,'error');
								return
							}
							if (response[1].code != 1) {
								$.messager.alert('提示',response[1].message,'error');
								return
							}
							$.each(response[0].datas , function(index , _data){
								if(_data.affiliate != 'codTotal' && _data.affiliate != 'stripeTotal' && _data.affiliate != 'allTotal'){
									datagridData.rows.push(_data);
								}else{
									datagridData.footer.push(_data);
								}
							});
							// 1
							datagridData.footer.push({'affiliate': '复购', 'orderCount': Math.round(response[1].datas * 10000)/100 + '%'});
							$('#affilateReport-datagrid').datagrid({
								data: datagridData,
								showFooter: true,
								pagination: false,
								columns:[[
									{field:'affiliate',title:'投放人',width:100,align:'center',formatter:function(value,row,index){
										if(value == 'codTotal') return 'COD汇总';
										if(value == 'stripeTotal') return 'Stripe汇总';
										if(value == 'allTotal') return '合计';
										return value;
									}},
									{field:'orderCount',title:'订单数量',width:100,align:'center'},
									{field:'orderSkuCount',title:'订单SKU数量',width:100,align:'center'},
									{field:'averageOrderSkuCount',title:'连单数',width:100,align:'center'},
									{field:'orderPrice',title:'订单价格',width:100,align:'center'},
									{field:'averageOrderPrice',title:'客单价',width:100,align:'center'},
									{field:'purchasePrice',title:'采购价',width:100,align:'center'}
								]],
								loadFilter: function(data){return data;}
							});
                        }).catch((e)=>{
                            console.log(e)
                        })
					}
				},
				init : function(){
					affilate_report.methods.initShopCombo();
					affilate_report.methods.initTimeSelect();
					affilate_report.methods.initDatagrid();
				}
			}
			affilate_report.init();
		</script>
	</body>
</html>
